package org.ssi.web.result;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 功能概述：<br>
 * 本类用于处理XML视图,采用JAXB技术处理
 * 
 * @author 
 */
public class XMLResult implements Result {
	/**   */
	private static final long serialVersionUID = 1L;

	private static final Log log = LogFactory.getLog(XMLResult.class);

	/** 引入实体的软件包列表采用":"分隔符 */
	private String packages = null;

	/** XML绑定的数据对象名称 */
	private String jaxbObj = "jaxbObj";

	/** 采用的默认编码 */
	private String encoding = "UTF-8";

	/** 采用单引号标识属性 */
	private boolean SingleQuotes = false;

	/**
	 * 
	 * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)
	 */
	public void execute(ActionInvocation invocation) throws Exception {
		// 取得值堆栈里的值
		ActionContext actionCtx = invocation.getInvocationContext();
		ValueStack stack = actionCtx.getValueStack();
		Object value = stack.findValue(this.jaxbObj.trim());
		if (this.packages == null) {
			this.packages = value.getClass().getPackage().getName();
		}
		// 编组为XML
		JAXBContext jc = JAXBContext.newInstance(packages);
		Marshaller m = jc.createMarshaller();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.setProperty("jaxb.encoding", encoding);

		m.marshal(value, out);
		String xmlStr = out.toString(encoding);
		if (this.isSingleQuotes()) {
			xmlStr = xmlStr.replace("\"", "'");
		}

		// 写XML字符串写入回复流
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml; charset=" + encoding);
		PrintWriter w;
		w = response.getWriter();
		w.write(xmlStr);
		w.flush();
		log.debug(xmlStr);
	}

	/**
	 * @return Returns the packages.
	 */
	public String getPackages() {
		return packages;
	}

	/**
	 * @param packages
	 *            The packages to set.
	 */
	public void setPackages(String packages) {
		this.packages = packages;
	}

	/**
	 * @return Returns the jaxbObj.
	 */
	public String getJaxbObj() {
		return jaxbObj;
	}

	/**
	 * @param jaxbObj
	 *            The jaxbObj to set.
	 */
	public void setJaxbObj(String jaxbObj) {
		this.jaxbObj = jaxbObj;
	}

	/**
	 * @return Returns the encoding.
	 */
	public String getEncoding() {
		return encoding;
	}

	/**
	 * @param encoding
	 *            The encoding to set.
	 */
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	/**
	 * @return Returns the singleQuotes.
	 */
	public boolean isSingleQuotes() {
		return SingleQuotes;
	}

	/**
	 * @param singleQuotes
	 *            The singleQuotes to set.
	 */
	public void setSingleQuotes(boolean singleQuotes) {
		SingleQuotes = singleQuotes;
	}
}
